package com.fh.shop.api.book.biz;

import com.fh.shop.api.book.mapper.IBookMapper;
import com.fh.shop.api.book.param.BookQueryParam;
import com.fh.shop.api.common.DataTableResult;
import com.fh.shop.util.ServerResponse;
import com.fh.shop.api.book.po.Book;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service("bookService")
public class IBookServiceImpl implements IBookService {
    @Resource
    private IBookMapper bookMapper;

    @Override
    public ServerResponse addBook(Book book) {
        bookMapper.insert(book);
        return ServerResponse.success();
    }

    @Override
    public ServerResponse findBookList() {
        List<Book> books = bookMapper.selectList(null);
        return ServerResponse.success(books);
    }

    @Override
    public ServerResponse deleteBook(Long id) {
        bookMapper.deleteById(id);
        return ServerResponse.success();
    }

    @Override
    public ServerResponse deleteBatch(String ids) {
        if (StringUtils.isNotEmpty(ids)){
            List<Long> idList = Arrays.stream(ids.split(",")).map(x -> Long.parseLong(x)).collect(Collectors.toList());
            bookMapper.deleteBatchIds(idList);
        }
        return ServerResponse.success();
    }

    @Override
    public ServerResponse updateBook(Book book) {
        bookMapper.updateById(book);
        return ServerResponse.success();
    }

    @Override
    public ServerResponse findBook(Long id) {
        Book book = bookMapper.selectById(id);
        return ServerResponse.success(book);
    }

    @Override
    public DataTableResult findList(BookQueryParam bookQueryParam) {
        Long count = bookMapper.findCount(bookQueryParam);
        List<Book> bookList = bookMapper.findList(bookQueryParam);
        return new DataTableResult(bookQueryParam.getDraw(),count,count,bookList);
    }
}
